package work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;
import util.ConnUtil;

import java.io.IOException;

/**
 * 一个队列中的消息要被多个消费者消费
 * @author TZL
 * @date 2020/8/13
 */
public class Publisher {
    public static final String QUEUE_NAME="WORK";

    @Test
    public void publish() throws Exception {
        //1. 获取连接对象
        Connection conn = ConnUtil.getConn();
        //2. 创建通道对象
        Channel channel = conn.createChannel();
        //3. 通过channe声明一个队列
            //String queue, boolean durable, boolean exclusive, boolean autoDelete,
            //                                 Map<String, Object> arguments
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);

        ////4. 通过channel发送消息到队列中  发送消息(10条)
        for (int i=0;i<10;i++){
            String msg1="work"+i;
            channel.basicPublish("",QUEUE_NAME,null,msg1.getBytes());
        }


        //5. 输出结果
        System.out.println("发送十条消息成功！");
        channel.close();
        conn.close();

    }

}
